package com.yuandian.management.controller;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuandian.api.customer_perception.po.ImportanceOperatorParams;
import com.yuandian.api.customer_perception.vo.ImportanceOperator;
import com.yuandian.api.customer_perception.vo.ImportanceOperatorVo;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.management.service.ConfImportanceOperatorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 客户感知-重点用户表-Controller
 * @author zfsn
 * @date 2023-3-15
 */
@Api(tags = "客户感知-重点用户表操作")
@RestController
@RequestMapping("/importanceOperator")
public class ConfImportanceOperatorController {

	@Autowired
	private ConfImportanceOperatorService confImportanceOperatorService;

	/**
	 * 客户感知-查询重点用户列表
	 */
	@ApiOperation(value = "客户感知-查询重点用户列表 ", notes = "客户感知-查询重点用户列表 ")
	@PostMapping("/queryImportanceOperatorList")
	public R<Object> queryImportanceOperatorList(@RequestBody ImportanceOperatorParams params) {
		return confImportanceOperatorService.findByPager(params);
	}


	/**
	 * 新增/修改重点用户
	 */
	@ApiOperation(value = "客户感知-新增/修改重点用户 ", notes = "客户感知-新增/修改重点用户")
	@PostMapping("/addOrUpdateImportanceOperator")
	public R<Object> addOrUpdateImportanceOperator(@RequestBody ImportanceOperatorVo vo) {
		// 判断picture头像大小是否超过65kb，超过则不允许新增或者修改
		if (vo.getPicture() != null && !("").equals(vo.getPicture())) {
			String picture = vo.getPicture();
			int i = picture.getBytes(StandardCharsets.UTF_8).length;
			if ((i / 1024) > 50) {
				R.failed("图片过大，无法上传，不能超过50kb");
			}
		}

		return vo.getId() == null ? confImportanceOperatorService.add(vo) : confImportanceOperatorService.updateImportanceOperator(vo);
	}



	/**
	 * 删除用户
	 */
	@GetMapping("/delete/{id}")
	public R<Object> deleteImportanceOperator(@PathVariable Long id) {
		if (confImportanceOperatorService.removeById(id)) {
			return R.ok();
		}
		return R.failed("删除失败");
	}

	/**
	 * 启用用户
	 */
	@GetMapping("/startImportanceOperator/{id}")
	public R<Object> startImportanceOperator(@NotNull(message = "用户id不能为空") @PathVariable Long id) {
		ImportanceOperator importanceOperator = confImportanceOperatorService.getById(id);
		if (importanceOperator == null) {
			return R.failed("不存在该用户");
		}
		importanceOperator.setStatus("0");
		if (confImportanceOperatorService.updateById(importanceOperator)) {
			return R.ok();
		}
		return R.failed("启用失败");
	}

	/**
	 * 停用用户
	 */
	@GetMapping("/stopImportanceOperator/{id}")
	public R<Object> stopImportanceOperator(@NotNull(message = "用户id不能为空") @PathVariable Long id) {
		ImportanceOperator importanceOperator = confImportanceOperatorService.getById(id);
		if (importanceOperator == null) {
			return R.failed("不存在该用户");
		}
		importanceOperator.setStatus("1");
		confImportanceOperatorService.updateById(importanceOperator);
		if (confImportanceOperatorService.updateById(importanceOperator)) {
			return R.ok();
		}
		return R.failed("禁用失败");
	}

	/**
	 * 根据id查询用户
	 */
	@GetMapping("/getById/{id}")
	public R<Object> getById(@NotNull(message = "用户id不能为空") @PathVariable Long id) {
		ImportanceOperator importanceOperator = confImportanceOperatorService.getById(id);
		return R.ok(importanceOperator);
	}

	@GetMapping("/getImportanceOperators")
	public R<List<ImportanceOperator>> getImportanceOperatorList(@RequestParam(value = "name") String name,
																 @RequestParam(value = "status") String status, @RequestParam(value = "operatorId") String operatorId) {
		List<ImportanceOperator> dataList = confImportanceOperatorService.list(new LambdaQueryWrapper<ImportanceOperator>()
				.eq(ImportanceOperator::getStatus, status)
				.like(StringUtils.isNotEmpty(name), ImportanceOperator::getName, name)
				.like(StringUtils.isNotEmpty(operatorId), ImportanceOperator::getOperatorId, operatorId)
				.orderByAsc(ImportanceOperator::getId));

		return R.ok(dataList);
	}


}
    